﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.IO;

public partial class Publish_ProductThumbnail : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        // get request id -- event id
        string id = "";
        id = Request.QueryString["id"];
        double width = 0;
        width = Convert.ToDouble(Request.QueryString["width"]);

        GssDO.DOProduct DO = getById(id);

        if (DO != null && DO.IsDeleted == 0)
        {
            // get file path
            string filePath = "../Photos/Product/" + DO.Image;

            // get file extention
            string fileExt = GlobalClassFE.getFileExtendtion(DO.Image).ToLower();

            // create an image object, using the filename we just retrieved
            System.Drawing.Image image = System.Drawing.Image.FromFile(Server.MapPath(filePath));

            int w = image.Width, h = image.Height;
            width = (w < width) ? w : width;
            double height = (width / w) * h;

            // create the actual thumbnail image
            System.Drawing.Image thumbnailImage = image.GetThumbnailImage((int)width, (int)height, new System.Drawing.Image.GetThumbnailImageAbort(ThumbnailCallback), IntPtr.Zero);

            // make a memory stream to work with the image bytes
            MemoryStream imageStream = new MemoryStream();

            // put the image into the memory stream
            if (fileExt == ".jpg")
                thumbnailImage.Save(imageStream, System.Drawing.Imaging.ImageFormat.Jpeg);
            else if (fileExt == ".gif")
                thumbnailImage.Save(imageStream, System.Drawing.Imaging.ImageFormat.Gif);
            else if (fileExt == ".png")
                thumbnailImage.Save(imageStream, System.Drawing.Imaging.ImageFormat.Png);
            else if (fileExt == ".bmp")
                thumbnailImage.Save(imageStream, System.Drawing.Imaging.ImageFormat.Bmp);

            // make byte array the same size as the image
            byte[] imageContent = new Byte[imageStream.Length];

            // rewind the memory stream
            imageStream.Position = 0;

            // load the byte array with the image
            imageStream.Read(imageContent, 0, (int)imageStream.Length);

            // return byte array to caller with image type
            Response.ContentType = GlobalClassFE.GetContentType(fileExt);
            Response.BinaryWrite(imageContent);
        }
    }

    /// <summary>
    /// Required, but not used
    /// </summary>
    /// <returns>true</returns>
    public bool ThumbnailCallback()
    {
        return true;
    }

    private GssDO.DOProduct getById(string id)
    {
        GssDO.DOProduct DO = null;
        try
        {
            GssBL.BLProduct BL = new GssBL.BLProduct();
            DO = BL.getProductByID(id);
        }
        catch (Exception ex)
        {
            string mess = ex.Message.ToString();
            DO = null;
        }
        return DO;
    }
}
